﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSLib.CodeFormating.Formatters;

namespace LuaCodeStyler
{
	public partial class TemplateFormatEditForm : Form
	{
		public TemplateFormatEditForm()
		{
			InitializeComponent();
		}

		private void buttonLexemRuleAdd_Click(object sender, EventArgs e)
		{
			LexemConfiguration result = CallTemplateLexemEditFormWithCheck(null,false);
			if (result != null) listBoxLexemRules.Items.Add(result);
		}

		private void buttonLexemRuleEdit_Click(object sender, EventArgs e)
		{
			LexemConfiguration selectedItem = listBoxLexemRules.SelectedItem as LexemConfiguration;
			if (selectedItem != null)
			{
				LexemConfiguration result = CallTemplateLexemEditFormWithCheck(selectedItem, true);
				if (result != null) 
				listBoxLexemRules.Items[listBoxLexemRules.SelectedIndex] = result;
			}
		}

		private void buttonLexemRuleRemove_Click(object sender, EventArgs e)
		{
			if (listBoxLexemRules.SelectedItem != null)
				listBoxLexemRules.Items.Remove(listBoxLexemRules.SelectedItem);
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			Close();
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			Close();
		}

		public FormatConfiguration FormatSettings 
		{
			get
			{
				FormatConfiguration result = new FormatConfiguration();
				result.Name = textBoxName.Text;
				result.Description = textBoxDescription.Text;
				result.Prefix = textBoxPrefix.Text;
				result.Postfix = textBoxPostfix.Text;
				foreach (var item in listBoxLexemRules.Items)
				{
					LexemConfiguration lexemSetting = item as LexemConfiguration;
					result.LexemConfigurationCollection.Add(lexemSetting);
				}

				return result;
			}
			set
			{
				if (value == null) return;
				FormatConfiguration editSettings = value.Clone();
				textBoxName.Text = editSettings.Name;
				textBoxDescription.Text = editSettings.Description;
				textBoxPrefix.Text = editSettings.Prefix;
				textBoxPostfix.Text = editSettings.Postfix;

				foreach (var item in editSettings.LexemConfigurationCollection)
					listBoxLexemRules.Items.Add(item);
			}
		}

		private LexemConfiguration CallTemplateLexemEditFormWithCheck(LexemConfiguration lcInput, bool edit)
		{
			LexemConfiguration result = lcInput;
			while (true)
			{
				result = CallTemplateLexemEditForm(result);
				if (result == null) return null;

				bool bRepeat = false;
				for (int i = 0; i < listBoxLexemRules.Items.Count; i++)
				{
					if (edit && i == listBoxLexemRules.SelectedIndex) continue;
					LexemConfiguration lc = listBoxLexemRules.Items[i] as LexemConfiguration;
					if (lc.LexemType == result.LexemType) bRepeat = true;
				}

				if (bRepeat)
					MessageBox.Show(
						"Format configuration already has rule for lexem type " + result.LexemType.ToString(),
						"Error",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
				else
				{
					return result;
				}
			}
		}
		private LexemConfiguration CallTemplateLexemEditForm(LexemConfiguration lc)
		{
			TemplateLexemEditForm f = new TemplateLexemEditForm();
			f.LexemConfiguration = lc;
			if (f.ShowDialog() == DialogResult.OK) return f.LexemConfiguration;
			return null;
		}
	}
}
